package com.itheima.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.PageVo;
import com.itheima.common.contants.RedisKeyConstants;
import com.itheima.question.dto.PageDto;
import com.itheima.question.dto.QuestionDto;
import com.itheima.question.mapper.QuestionMapper;
import com.itheima.question.pojo.Question;
import com.itheima.question.service.IQuestionService;
import com.itheima.question.vo.QueryQuestionVo;
import com.itheima.question.vo.QuestionVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl implements IQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Override
    public boolean addQuestion(QuestionDto questionDto) {

        Question question = new Question();
        BeanUtils.copyProperties(questionDto,question);

        boolean result = questionMapper.insert(question) > 0;

        //文字审核
        String id = question.getId();
        //将题目id存入redis的set集合中(audi_question)
        redisTemplate.opsForSet().add(RedisKeyConstants.AUDI_QUESTION,id);

        String pid = question.getPicture();
        //保存到redis
        redisTemplate.opsForSet().add(RedisKeyConstants.USE_PIC,pid);

        return result;
    }

    @Override
    public PageVo<QuestionVo> queryByPage(PageDto pageDto) {
        Page<Question> page = new Page<>(pageDto.getCurrentPage(),pageDto.getPageSize());

        QueryWrapper<Question> queryWrapper = new QueryWrapper<>();
        if (pageDto.getState()!=null){
            queryWrapper.like("state",pageDto.getState());
        }
        if (pageDto.getSubject()!=null){
            queryWrapper.like("subject",pageDto.getSubject());
        }

        IPage<Question> questionPage = questionMapper.selectPage(page,queryWrapper);

        List<QuestionVo> collect = questionPage.getRecords().stream().map(
                question -> {
                    QuestionVo questionVo = new QuestionVo();
                    BeanUtils.copyProperties(question, questionVo);
                    return questionVo;
                }
        ).collect(Collectors.toList());
        return new PageVo<>(collect, (int) questionPage.getTotal());
    }

    @Override
    public QueryQuestionVo queryById(String questionId) {

        Question question = questionMapper.selectById(questionId);
        QueryQuestionVo queryQuestionVo = new QueryQuestionVo();
        if (question != null) {
            BeanUtils.copyProperties(question, queryQuestionVo);
        }
        return queryQuestionVo;
    }

    @Override
    public boolean deleteById(String questionId) {

        return questionMapper.deleteById(questionId) > 0;
    }

    @Override
    public boolean editQuestion(QueryQuestionVo queryQuestionVo) {

        Question question = new Question();

        if (queryQuestionVo != null){
            BeanUtils.copyProperties(queryQuestionVo,question);
            return questionMapper.updateById(question) > 0;
        }

        return false;
    }
}
